/*
 * topological-hex, a program to compute combinatorial hexahedral meshes.
 *
 * Copyright (C) <2018> <Université catholique de Louvain (UCL), Belgique>
 *
 * List of the contributors to the development, description and complete
 * License: see LICENSE file.
 *
 * This program (topological-hex) is free software:
 * you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (see COPYING file).  If not,
 * see <http://www.gnu.org/licenses/>.
 */

#include <check.h>
#include "lower_bound.h"
#include "compatibility.h"
#include "parallel_dfs.h"

static
void on_solution(const solver *solver, uint32_t num_hex, void *data) {
  for (size_t i = 0; i < num_hex; i++) {
    for (size_t j = 0; j < i; j++)
      ck_assert(hex_compatibility(solver->hexes + 8 * i, solver->hexes + 8 * j));
  }
}

#define LOAD_LOWER_BOUNDS (1 << 0)
#define LOAD_PRECOMPUTED  (1 << 1)

static void test_input(const char *file,
                       uint32_t num_hexes, uint32_t num_extra_vertices,
                       uint32_t flags) {
  if (flags & LOAD_LOWER_BOUNDS)
    ck_assert(load_lower_bounds(INPUT_DIR "/shellable-lower-bounds.dat") == SUCCESS);

  if (flags & LOAD_PRECOMPUTED)
    ck_assert(load_known_solutions(INPUT_DIR "/precomputed-8.dat") == SUCCESS);

  mesh mesh;
  ck_assert(mesh_load(&mesh, file) == SUCCESS);
  ck_assert(mesh_reserve(&mesh, num_hexes) == SUCCESS);

  solver solver;
  ck_assert(solver_init(&solver, &mesh,
                        mesh.num_vertices + num_extra_vertices,
                        SOLVER_SHELLABLE_ONLY) == SUCCESS);

  solver_compute_symmetries(&solver);

  ck_assert(solver_run_parallel(&solver, on_solution, NULL) == SUCCESS);

  solver_release(&solver);
  mesh_release(&mesh);

  release_known_solutions();
  release_lower_bounds();
}

START_TEST(test_lower_bounds) {
  test_input(INPUT_DIR "/single-hex.mesh", 40, 10, LOAD_LOWER_BOUNDS);
  test_input(INPUT_DIR "/spindle.mesh", 40, 8, LOAD_LOWER_BOUNDS);
  test_input(INPUT_DIR "/erickson-buffer-a.mesh", 40, 5, LOAD_LOWER_BOUNDS);
} END_TEST

START_TEST(test_known_solutions) {
  test_input(INPUT_DIR "/single-hex.mesh", 40, 10, LOAD_PRECOMPUTED);
  test_input(INPUT_DIR "/spindle.mesh", 40, 8, LOAD_PRECOMPUTED);
  test_input(INPUT_DIR "/erickson-buffer-a.mesh", 40, 5, LOAD_PRECOMPUTED);
} END_TEST

Suite *lower_bound_suite(void) {
  Suite *s = suite_create("Lower Bound");

  TCase *tcase = tcase_create("Lower bounds");
  tcase_add_test(tcase, test_lower_bounds);
  suite_add_tcase(s, tcase);

  tcase = tcase_create("Known solutions");
  tcase_add_test(tcase, test_known_solutions);
  suite_add_tcase(s, tcase);

  return s;
}
